package controller

import (
	"encoding/json"
	"hft/Http/Models/request/front"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	maptool "hft/tools/map_tool"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Map struct{}

func (*Map) SearchTips(c *gin.Context) {
	var q = c.DefaultQuery("q", "")
	var city = c.DefaultQuery("city", "北京")
	var poiType = c.DefaultQuery("type", "")
	var location = c.DefaultQuery("location", "")

	data, err := services.MapServices.SearchTips(q, city, poiType, location)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*Map) GetCity(c *gin.Context) {

	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	data, err := services.MapServices.GetCity(id)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)

}

func (*Map) SearchV2(c *gin.Context) {

	var searchV2 front.SearchV2

	c.ShouldBind(&searchV2)

	data := maptool.AmapApi.SearchV2(searchV2.Keyword, searchV2.Types, searchV2.Region, searchV2.CityLimit, searchV2.PageNum, searchV2.PageSize)

	var newData *interface{}

	json.Unmarshal([]byte(data), &newData)

	utils.OkWithData(newData, c)

}

func (*Map) SearchAround(c *gin.Context) {
	var searchAround front.SearchAround

	c.ShouldBind(&searchAround)

	data := maptool.AmapApi.SearchAround(searchAround.Keyword, searchAround.Types, searchAround.Location, searchAround.Radius, searchAround.Region, searchAround.CityLimit, searchAround.PageNum, searchAround.PageSize)

	var newData *interface{}

	json.Unmarshal([]byte(data), &newData)

	utils.OkWithData(newData, c)
}

func (*Map) InputTips(c *gin.Context) {

	var inputtips front.InputTips

	c.ShouldBind(&inputtips)

	data := maptool.AmapApi.InputTipsV2(inputtips.Keyword, inputtips.Type, inputtips.Location, inputtips.City, inputtips.CityLimit)

	var newData *interface{}

	json.Unmarshal([]byte(data), &newData)

	utils.OkWithData(newData, c)

}

var MapController = new(Map)
